package com.fangzc;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.util.function.UnaryOperator;

public class CodeGen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/ry-vue?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = globalConfig();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig globalConfig() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        UnaryOperator<String> tableFormat = (e) -> e.split("，")[0].replace("表", "");
        //设置注解生成配置
        globalConfig.getJavadocConfig()
                .setTableCommentFormat(tableFormat);

        //设置根包
        globalConfig.getPackageConfig()
                .setSourceDir(System.getProperty("user.dir") + "/src/main/java")
                .setMapperXmlPath(System.getProperty("user.dir") + "/src/main/java/resources/mapper")
                .setBasePackage("com.fangzc.codegen");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setOverwriteEnable(true)
                .setClassSuffix("PO")
                .setWithLombok(true);
        //配置生成 tableDef
        globalConfig.enableTableDef()
                .setClassPrefix("T")
                .setClassSuffix("")
                .setOverwriteEnable(true);
        //配置生成 mapper
        globalConfig.enableMapper()
                .setClassPrefix("I");
        //配置生成 mapperXml
        globalConfig.enableMapperXml();

        //配置生成 service
        globalConfig.enableService();
        //配置生成 serviceImpl
        globalConfig.enableServiceImpl();
        //配置生成 controller
        globalConfig.enableController();

        //配置生成 package-info.java
//        globalConfig.enablePackageInfo();
        //设置表前缀和只生成哪些表
//        globalConfig.setTablePrefix("tb_");
//        globalConfig.setGenerateTable("account", "account_session");

        //可以单独配置某个列
//        ColumnConfig columnConfig = new ColumnConfig();
//        columnConfig.setColumnName("tenant_id");
//        columnConfig.setLarge(true);
//        columnConfig.setVersion(true);
//        globalConfig.setColumnConfig("account", columnConfig);

        return globalConfig;
    }

}
